import wepy from "wepy";
import util from "./utils";
import md5 from "./md5";
import tip from "./tip";

const API_SECRET_KEY = "www.mall.cycle.com"
// const TIMESTAMP = util.getCurrentTime()
// const SIGN = md5.hex_md5((TIMESTAMP + API_SECRET_KEY).toLowerCase())
let _Authorization = "user-sessionId";

const wxRequest = async( params = {}, url) => {
    tip.loading();
    let data = params.query || {};
    // data.sign = SIGN;
    // data.time = TIMESTAMP;
    let item = {},
        token = wepy.getStorageSync("token");
    item['Content-Type'] = "application/json";
    if(token){
        item['Authorization'] = "Bearer "+ token;
    }
    let res = await wepy.request({
        url: url,
        method: params.method || "GET",
        data: data,
        header: item,
    });
    tip.loaded();
    if(res.data.code == "1013"||res.data.code == "1012"||res.data.code == "1010"){
        wepy.clearStorageSync();
        wepy.navigateTo({
            url: '/pages/personalCenter/login'
        })
    }else {
        return res;
    }
};


module.exports = {
    wxRequest
}
